package main

import (
	"context"
	"fmt"
	pb "gostu/grpc_stu/try_and_study/hello_grpc/proto"
	"net"

	"google.golang.org/grpc"
	"google.golang.org/grpc/grpclog"
)

const (
	Address = "127.0.0.1:50052"
)

// 定义这个空结构体
type helloService struct{}

var HelloService = helloService{}

// 空结构体实现了接口
func (h helloService) SayHello(c context.Context, in *pb.HelloRequest) (*pb.HelloResponse, error) {
	resp := new(pb.HelloResponse)
	resp.Message = fmt.Sprintf("hello %s", in.Name)
	return resp, nil
}

func main() {
	listen, err := net.Listen("tcp", Address)
	if nil != err {
		grpclog.Fatalf("Failed to listen %v", Address)
	}

	// 实例化 grpc的服务
	s := grpc.NewServer()

	// 注册服务  -- 一般来说，rpc就是需要服务发现的
	pb.RegisterHelloServer(s, HelloService)

	// grpclog.Println("listen on" + Address)
	grpclog.Infoln("listen on" + Address)

	fmt.Println("listen on" + Address)
	s.Serve(listen)
}
